{
 "metadata": {
  "name": "",
  "signature": "sha256:16b7c3b77d09a85ff2c0b40b6eb9d6817cdbf508332fe7cddf207722672fd3f2"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = np.genfromtxt('testSet.txt')\n",
      "print(type(data))\n",
      "x = data[:,0]\n",
      "y = data[:,1]\n",
      "plt.scatter(x,y)\n",
      "plt.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<class 'numpy.ndarray'>\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAFVhJREFUeJzt3W+IXNd5x/HfI3mL19jxtnhL8EqK\nDC0CYcdRMxiDXqTIJnJiExnXoUmbQMgLvUnATt01UgwFvygWbEnyIoEg2kIhBruOZbX5U2QbOZQa\nXLKypCiyrWBcy/HaIRviTVJ7G6+kpy92V9odzZ87M+fee8653w8IrNF45szcmWee+5znnGvuLgBA\nPjbUPQAAQFgEdgDIDIEdADJDYAeAzBDYASAzBHYAyAyBHQAyQ2AHgMwQ2AEgM1fU8aTXXXedb926\ntY6nBoBkHTt27FfuPtnvfrUE9q1bt2p2draOpwaAZJnZ2SL3oxQDAJkhsANAZgjsAJAZAjsAZIbA\nDgCZqaUrBkB8Dh+f08yRM3prYVHXT4xrevc23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Xd/zd3f\nl/SYpD01j6kU7v62u7+48t+/03Kwy3q/ZjPbJOlOSf9Y91hCILCXxMz2SJpz95N1j6UGX5T0H3UP\nIrApST9f8/c3lXmwkyQz2ypph6T/rnckpfuGlpOwC3UPJAQutDECM3tW0gc7/NNDkr6q5TJMNnq9\nXnf/t5X7PKTlU/lHqxwbwjOzqyU9Kel+d/9t3eMpi5ndJemX7n7MzP687vGEQGAfgbvf3ul2M7tJ\n0g2STpqZtFyWeNHMbnH3X1Q4xKC6vd5VZvYFSXdJus3z66Odk7R5zd83rdyWJTMb03JQf9TdD9U9\nnpLtlPQpM/ukpCslfcDMvuPun6t5XEOjj70CZva6pJa7x77B0NDM7A5JX5P0MXefr3s8oZnZFVqe\nFL5NywH9x5L+yt1P1zqwEthyNvIvkn7t7vfXPZ4qrWTsf+vud9U9llFQY0co35R0jaRnzOyEmX27\n7gGFtDIx/GVJR7Q8mfivOQb1FTslfV7SrpVjeWIlm0UiyNgBIDNk7ACQGQI7AGSGwA4AmSGwA0Bm\nCOwAkBkCOwBkhsAOAJkhsANAZv4fS2aYwZb+IuoAAAAASUVORK5CYII=\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7feae3638b00>"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def compute_distance(centers, data, record):\n",
      "    for i in range(len(data)):\n",
      "        maxValue = float('inf')\n",
      "        index = -1\n",
      "        for j in range(len(centers)):\n",
      "            #D = (centers[j][0]-data[i][0])**2 + (centers[j][1]-data[i][1])**2\n",
      "            #print (D, maxValue)\n",
      "            D = np.sqrt(sum((centers[j]-data[i])**2))\n",
      "            try:\n",
      "                if D < maxValue:\n",
      "                    maxValue = D\n",
      "                    index = j\n",
      "            except ValueError:\n",
      "                print (centers)\n",
      "                print (D, maxValue)\n",
      "                break\n",
      "        record[index][0] += data[i][0]\n",
      "        record[index][1] += data[i][1]\n",
      "        record[index][2] += 1\n",
      "    return record\n",
      "\n",
      "def updata_centers(centers, record):\n",
      "    change = False\n",
      "    for i in range(len(record)):\n",
      "        if record[i][2]:\n",
      "            record[i][0] /= record[i][2]\n",
      "            record[i][1] /= record[i][2]\n",
      "        if centers[i][0] != record[i][0] or centers[i][1] != record[i][1]:\n",
      "            change = True\n",
      "            centers[i][0] = record[i][0] \n",
      "            centers[i][1] = record[i][1]\n",
      "    return change\n",
      "\n",
      "def k_means(k, data, epoches):\n",
      "    len1 = len(data[0])\n",
      "    centers = [[0.0]*len1 for _ in range(k)]\n",
      "    \n",
      "    for i in range(epoches):\n",
      "        record = [[0.0]*(len1+1) for _ in range(k)]\n",
      "        compute_distance(centers, data, record)\n",
      "        isChangge = updata_centers(centers, record)\n",
      "        if not isChangge:\n",
      "            print (\"Cluster complete!\")\n",
      "            break\n",
      "    return centers"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "centers = k_means(4, data, 100)\n",
      "centers"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Cluster complete!\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "[[-3.5397388947368418, -2.893843263157895],\n",
        " [2.6265298999999995, 3.1086801500000001],\n",
        " [-2.4615431499999998, 2.7873755500000006],\n",
        " [2.6507736666666668, -2.790190285714286]]"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "centers = np.array(centers)\n",
      "print (type(centers[:]))\n",
      "\n",
      "plt.scatter(x,y)\n",
      "plt.scatter(centers[:,0],centers[:,1],c='r', marker='x')\n",
      "plt.show"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<class 'numpy.ndarray'>\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "<function matplotlib.pyplot.show>"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAF5FJREFUeJzt3X2MXFd5x/HfY7MoG0IyVDFFXts4\naisjKyGYrCIkVwLZCAcSYiuFFgpUyGr8D1TONmxkEylyUlVxWcRiCVTkAFURVqGBxA0vrRNiUEUk\nKtbYJoTEKIUE2IAwIptQe4PX9tM/Zte7O57dvTNzX8459/uRrHjHkztn3p597nOec665uwAA6VhW\n9QAAAPkisANAYgjsAJAYAjsAJIbADgCJIbADQGII7ACQGAI7ACSGwA4AiXlZFQ965ZVX+tq1a6t4\naACI1pEjR37r7iuWul8lgX3t2rUaGxur4qEBIFpm9myW+1GKAYDEENgBIDEEdgBIDIEdABJDYAeA\nxFTSFQMgPAePjmvk0Ak9NzGplY1+DW9Zp20bBqoeFrpAYAegg0fHtfuBxzU5dU6SND4xqd0PPC5J\nBPcIEdgBaOTQiQtBfcbk1DmNHDqRTGCv0xkJgR2AnpuY7Oj22NTtjITJUwBa2ejv6PbYLHZGkiIC\nOwANb1mn/r7l827r71uu4S3rKhpRvlI/I2lFYAegbRsGdO8t12ig0S+TNNDo1723XJNMmSL1M5JW\n1NgBSGoG91QCeavhLevm1diltM5IWhHYASRv5hcWXTEAkJCUz0haUWMHgMQQ2AEgMQR2AEgMgR0A\nEkNgB4DEENgBIDEEdgBIDIEdABJDYAeAxLDyFKiROl1sos4I7CgMQSQsdbvYRJ1RikEhZoLI+MSk\nXLNB5ODR8aqHVlt1u9hEnRHYUQiCSHjqdrGJOiOwoxAEkfDU7WITdUZgRyEIIuFJ/fJ3mJVbYDez\n5WZ21My+ntcxES+CSH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dnSH7g8V0pK8hKBC6jT863Tc5Xq9XxjeK6vdfclt8etJLCnzszGsuznkIo6Pd86PVepXs83\npedKKQYAEkNgB4DEENiLsb/qAZSsTs+3Ts9VqtfzTea5UmMHgMSQsQNAYgjsBTOz283MzezKqsdS\nJDMbMbOnzOyHZvagmTWqHlPezOwGMzthZk+b2a6qx1MUM1ttZt82sx+b2RNmtrPqMZXBzJab2VEz\n+3rVY+kVgb1AZrZa0tsk/bzqsZTgEUlXu/vrJf1E0u6Kx5MrM1su6dOS3i5pvaT3mtn6akdVmLOS\nbnf39ZLeJOlDCT/XuXZKerLqQeSBwF6sUUl3SEp+IsPdH3b3s9M/fk/SqirHU4DrJT3t7j919zOS\nviRpa8VjKoS7/8rdfzD999+rGeyS3q/ZzFZJulHSZ6seSx4I7AUxs62Sxt39eNVjqcB2Sf9Z9SBy\nNiDpF3N+/qUSD3aSZGZrJW2Q9D/VjqRwn1QzCTtf9UDywIU2emBm35L0mjb/dKekj6pZhknGYs/X\n3f9j+j53qnkqf6DMsSF/ZnaZpK9Kus3dX6x6PEUxs5sk/cbdj5jZW6oeTx4I7D1w97e2u93MrpF0\nlaTjZiY1yxI/MLPr3f3XJQ4xVws93xlm9kFJN0na7On10Y5LWj3n51XTtyXJzPrUDOoH3P2BqsdT\nsI2Sbjazd0i6RNLlZvZFd39/xePqGn3sJTCzZyQNunvoGwx1zcxukPQJSW9295NVjydvZvYyNSeF\nN6sZ0L8v6a/d/YlKB1YAa2Yj/yrpd+5+W9XjKdN0xv4Rd7+p6rH0gho78vIpSa+U9IiZHTOzz1Q9\noDxNTwx/WNIhNScT/z3FoD5to6QPSNo0/V4em85mEQkydgBIDBk7ACSGwA4AiSGwA0BiCOwAkBgC\nOwAkhsAOAIkhsANAYgjsAJCY/wc7oXGvpfUJygAAAABJRU5ErkJggg==\n",
       "text": [
        "<matplotlib.figure.Figure at 0x7feae1427c50>"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def showCluster(data, k, centers, clusterData):\n",
      "    numSamples, dim = data.shape\n",
      "    if dim != 2:\n",
      "        print (\"dimension of your data is not 2\")\n",
      "        return 1\n",
      "    mark = ['or', 'ob', 'og', 'r', '+r', 'sr', 'dr', '<r', 'pr']\n",
      "    if k > len(mark):\n",
      "        print (\"Your k is too large.\")\n",
      "        return 1\n",
      "    \n",
      "    for i in range(numSamples):\n",
      "        markIndex = int(clusterData[i,0])\n",
      "        plt.plot(data[i,0], data[i,1], mark[markIndex])\n",
      "    \n",
      "    mark = ['*r', '*b', '*g', '*k', '+b', 'sb', 'db']\n",
      "    for i in range(k):\n",
      "        plt.plot(centers[i,0], centers[i,1], mark[i], markersize=20)\n",
      "    plot.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}